// * 
// * ExPOUND Project Properties
// * 
// * 
// * @copyright 2007 Loughborough University
// * @license http://www.gnu.org/licenses/gpl.txt
// * @version 0.0.0.1
// * @since Sept 2009
// * /

import classes.ProjectProperties;

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;

import mx.controls.Alert;
import mx.core.Application;
import mx.managers.PopUpManager;


//import mxml_views.project_properties;

public var _projectPropertiesWindow:project_properties;

private function displayNewForm():void {
	_projectPropertiesWindow = project_properties(PopUpManager.createPopUp(this, project_properties, true));
	PopUpManager.centerPopUp(_projectPropertiesWindow);
	//add a listener for the settings being okayed
	_projectPropertiesWindow["btn_okay"].addEventListener("click", SaveProjectProperties);
}

private function displayForm():void {
	_projectPropertiesWindow = project_properties(PopUpManager.createPopUp(this, project_properties, true));
	PopUpManager.centerPopUp(_projectPropertiesWindow);
	
	//populate the form with data
	_projectPropertiesWindow.txt_proj_name.text = Application.application._ProjectProperties._projectName;
	_projectPropertiesWindow.txt_author.text = Application.application._ProjectProperties._projectAuthor;
	_projectPropertiesWindow.txtDescription.text = Application.application._ProjectProperties._projectDescription;
	_projectPropertiesWindow.academic_level = Application.application._ProjectProperties._projectAcademicLevel;
	_projectPropertiesWindow.txtKeywrds.text = Application.application._ProjectProperties._projectKeywords;
	
	//add a listener for the settings being okayed
	_projectPropertiesWindow["btn_okay"].addEventListener("click", SaveProjectProperties);
}


private function SaveProjectProperties(event:Event):void{
	
	//destroy all of the class instances and information held by the tool at present.
	Application.application._ProjectProperties = null;
	Application.application._ProjectProperties = new ProjectProperties();
	
	//remove traces of the previous project from the interface
	Application.application.layers.Layers.dataProvider = null;
	Application.application.MainCanvas.removeAllChildren();
	Application.application.timelines.PageList.dataProvider = null;
	
	//label the application area with the project name
	Application.application.Expound_Container.title = "Project name: " + _projectPropertiesWindow.txt_proj_name.text;
	
	//populate the instance of the project properties class with the user entered information
	Application.application._ProjectProperties._projectName = _projectPropertiesWindow.txt_proj_name.text;
	Application.application._ProjectProperties._projectAuthor = _projectPropertiesWindow.txt_author.text;
	Application.application._ProjectProperties._projectDescription = _projectPropertiesWindow.txtDescription.text;
	//Application.application._ProjectProperties._projectAcademicLevel = _projectPropertiesWindow.academic_level;
	
	for each (var level:Object in _projectPropertiesWindow.academic_level){
//Alert.show(level.toString());		
	}
	
	
	
	Application.application._ProjectProperties._projectKeywords = _projectPropertiesWindow.txtKeywrds.text;
		
	//remove the pop-up window
	PopUpManager.removePopUp(_projectPropertiesWindow);
	
	//@todo: open up the interface and make elements accessable now we have a project
		
	SaveToServer("new");
		
}

/*
	Function to save the data to the server
*/
public function SaveToServer(SavePoint:String):void{
	//set up a vaariable to hold the xml and call the function in the project prperties class that wil 
	//write out and return the XML
	var returnedXML:XML = Application.application._ProjectProperties.WriteXML();
Alert.show(returnedXML);	

	var saveURL:String = "";
	if (SavePoint == "new"){
		saveURL = Application.application.serverURL + "save/new_project.php";
	}else if (SavePoint == "update"){
		saveURL = Application.application.serverURL + "save/savepoint.php"
	}

Alert.show(saveURL);	
	
	var variables:URLVariables = new URLVariables();
	var myxml:String = returnedXML.toString();	
	variables.myxml = myxml;
	
	var req:URLRequest = new URLRequest(saveURL);
	req.method = URLRequestMethod.POST;
	req.data = variables;
	//req.contentType = "text/xml";
	//req.data = returnedXML;
	
	var loader:URLLoader = new URLLoader();
	
	loader = new URLLoader(req);
	loader.addEventListener(Event.COMPLETE, SentHandler);
	loader.addEventListener(IOErrorEvent.IO_ERROR, SentErrorHandler);
	//loader.dataFormat = URLLoaderDataFormat.VARIABLES;
	loader.load(req);

}

private function SentHandler(event:Event):void{
	//as we know that the data was sent (and we are reliant on the PHP running properly)
	//out put to the screen at the top after the name the date and time the project was last saved.
	var lastSaved:Date = new Date();

	Application.application.Expound_Container.title = "Project name: " + Application.application._ProjectProperties._projectName + " (last saved: " + lastSaved.toDateString() + ")";
	
}

private function SentErrorHandler(errorEvent:IOErrorEvent):void{
	//catch if the XML transfere fails and let the user know
	Alert.show("There has been an error. Your new project has not been saved to the server.");
}